

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.multi_objective.study &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.multi_objective.study</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.multi_objective.study 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">types</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Callable</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Iterable</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Tuple</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Type</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span>

<span class="kn">import</span> <span class="nn">optuna</span>
<span class="kn">from</span> <span class="nn">optuna._experimental</span> <span class="kn">import</span> <span class="n">experimental</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">logging</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">multi_objective</span>
<span class="kn">from</span> <span class="nn">optuna.storages</span> <span class="kn">import</span> <span class="n">BaseStorage</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">Study</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">StudyDirection</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">FrozenTrial</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">Trial</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">TrialState</span>

<span class="n">ObjectiveFuncType</span> <span class="o">=</span> <span class="n">Callable</span><span class="p">[[</span><span class="s2">&quot;multi_objective.trial.MultiObjectiveTrial&quot;</span><span class="p">],</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">]]</span>
<span class="n">CallbackFuncType</span> <span class="o">=</span> <span class="n">Callable</span><span class="p">[</span>
    <span class="p">[</span>
        <span class="s2">&quot;multi_objective.study.MultiObjectiveStudy&quot;</span><span class="p">,</span>
        <span class="s2">&quot;multi_objective.trial.FrozenMultiObjectiveTrial&quot;</span><span class="p">,</span>
    <span class="p">],</span>
    <span class="kc">None</span><span class="p">,</span>
<span class="p">]</span>

<span class="n">_logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">get_logger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<span class="c1"># TODO(ohta): Reconsider the API design.</span>
<span class="c1"># See https://github.com/optuna/optuna/pull/1054/files#r407255282 for the detail.</span>
<span class="c1">#</span>
<span class="c1"># TODO(ohta): Consider to add `objective_labels` argument.</span>
<span class="c1"># See: https://github.com/optuna/optuna/pull/1054#issuecomment-616382152</span>
<div class="viewcode-block" id="create_study"><a class="viewcode-back" href="../../../reference/multi_objective/study.html#optuna.multi_objective.study.create_study">[文档]</a><span class="nd">@experimental</span><span class="p">(</span><span class="s2">&quot;1.4.0&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">create_study</span><span class="p">(</span>
    <span class="n">directions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">],</span>
    <span class="n">study_name</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="n">storage</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="n">BaseStorage</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="n">sampler</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="s2">&quot;multi_objective.samplers.BaseMultiObjectiveSampler&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="n">load_if_exists</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;multi_objective.study.MultiObjectiveStudy&quot;</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Create a new :class:`~optuna.multi_objective.study.MultiObjectiveStudy`.</span>

<span class="sd">    Args:</span>
<span class="sd">        directions:</span>
<span class="sd">            Optimization direction for each objective value.</span>
<span class="sd">            Set ``minimize`` for minimization and ``maximize`` for maximization.</span>
<span class="sd">        study_name:</span>
<span class="sd">            Study&#39;s name. If this argument is set to None, a unique name is generated</span>
<span class="sd">            automatically.</span>
<span class="sd">        storage:</span>
<span class="sd">            Database URL. If this argument is set to None, in-memory storage is used, and the</span>
<span class="sd">            :class:`~optuna.study.Study` will not be persistent.</span>

<span class="sd">            .. note::</span>
<span class="sd">                When a database URL is passed, Optuna internally uses `SQLAlchemy`_ to handle</span>
<span class="sd">                the database. Please refer to `SQLAlchemy&#39;s document`_ for further details.</span>
<span class="sd">                If you want to specify non-default options to `SQLAlchemy Engine`_, you can</span>
<span class="sd">                instantiate :class:`~optuna.storages.RDBStorage` with your desired options and</span>
<span class="sd">                pass it to the ``storage`` argument instead of a URL.</span>

<span class="sd">             .. _SQLAlchemy: https://www.sqlalchemy.org/</span>
<span class="sd">             .. _SQLAlchemy&#39;s document:</span>
<span class="sd">                 https://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls</span>
<span class="sd">             .. _SQLAlchemy Engine: https://docs.sqlalchemy.org/en/latest/core/engines.html</span>

<span class="sd">        sampler:</span>
<span class="sd">            A sampler object that implements background algorithm for value suggestion.</span>
<span class="sd">            If :obj:`None` is specified,</span>
<span class="sd">            :class:`~optuna.multi_objective.samplers.NSGAIIMultiObjectiveSampler` is used</span>
<span class="sd">            as the default. See also :class:`~optuna.multi_objective.samplers`.</span>
<span class="sd">        load_if_exists:</span>
<span class="sd">            Flag to control the behavior to handle a conflict of study names.</span>
<span class="sd">            In the case where a study named ``study_name`` already exists in the ``storage``,</span>
<span class="sd">            a :class:`~optuna.exceptions.DuplicatedStudyError` is raised if ``load_if_exists`` is</span>
<span class="sd">            set to :obj:`False`.</span>
<span class="sd">            Otherwise, the creation of the study is skipped, and the existing one is returned.</span>

<span class="sd">    Returns:</span>
<span class="sd">        A :class:`~optuna.multi_objective.study.MultiObjectiveStudy` object.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># TODO(ohta): Support pruner.</span>
    <span class="n">mo_sampler</span> <span class="o">=</span> <span class="n">sampler</span> <span class="ow">or</span> <span class="n">multi_objective</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">NSGAIIMultiObjectiveSampler</span><span class="p">()</span>
    <span class="n">sampler_adapter</span> <span class="o">=</span> <span class="n">multi_objective</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">_MultiObjectiveSamplerAdapter</span><span class="p">(</span><span class="n">mo_sampler</span><span class="p">)</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">directions</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;`directions` must be a list or other iterable types.&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="n">d</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;minimize&quot;</span><span class="p">,</span> <span class="s2">&quot;maximize&quot;</span><span class="p">]</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">directions</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;`directions` includes unknown direction names.&quot;</span><span class="p">)</span>

    <span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">(</span>
        <span class="n">study_name</span><span class="o">=</span><span class="n">study_name</span><span class="p">,</span>
        <span class="n">storage</span><span class="o">=</span><span class="n">storage</span><span class="p">,</span>
        <span class="n">sampler</span><span class="o">=</span><span class="n">sampler_adapter</span><span class="p">,</span>
        <span class="n">pruner</span><span class="o">=</span><span class="n">optuna</span><span class="o">.</span><span class="n">pruners</span><span class="o">.</span><span class="n">NopPruner</span><span class="p">(),</span>
        <span class="n">load_if_exists</span><span class="o">=</span><span class="n">load_if_exists</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="n">study</span><span class="o">.</span><span class="n">set_system_attr</span><span class="p">(</span><span class="s2">&quot;multi_objective:study:directions&quot;</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">directions</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">MultiObjectiveStudy</span><span class="p">(</span><span class="n">study</span><span class="p">)</span></div>


<div class="viewcode-block" id="load_study"><a class="viewcode-back" href="../../../reference/multi_objective/study.html#optuna.multi_objective.study.load_study">[文档]</a><span class="nd">@experimental</span><span class="p">(</span><span class="s2">&quot;1.4.0&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">load_study</span><span class="p">(</span>
    <span class="n">study_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
    <span class="n">storage</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">BaseStorage</span><span class="p">],</span>
    <span class="n">sampler</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="s2">&quot;multi_objective.samplers.BaseMultiObjectiveSampler&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;multi_objective.study.MultiObjectiveStudy&quot;</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Load the existing :class:`MultiObjectiveStudy` that has the specified name.</span>

<span class="sd">    Args:</span>
<span class="sd">        study_name:</span>
<span class="sd">            Study&#39;s name. Each study has a unique name as an identifier.</span>
<span class="sd">        storage:</span>
<span class="sd">            Database URL such as ``sqlite:///example.db``. Please see also the documentation of</span>
<span class="sd">            :func:`~optuna.multi_objective.study.create_study` for further details.</span>
<span class="sd">        sampler:</span>
<span class="sd">            A sampler object that implements background algorithm for value suggestion.</span>
<span class="sd">            If :obj:`None` is specified,</span>
<span class="sd">            :class:`~optuna.multi_objective.samplers.RandomMultiObjectiveSampler` is used</span>
<span class="sd">            as the default. See also :class:`~optuna.multi_objective.samplers`.</span>

<span class="sd">    Returns:</span>
<span class="sd">        A :class:`~optuna.multi_objective.study.MultiObjectiveStudy` object.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">mo_sampler</span> <span class="o">=</span> <span class="n">sampler</span> <span class="ow">or</span> <span class="n">multi_objective</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">RandomMultiObjectiveSampler</span><span class="p">()</span>
    <span class="n">sampler_adapter</span> <span class="o">=</span> <span class="n">multi_objective</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">_MultiObjectiveSamplerAdapter</span><span class="p">(</span><span class="n">mo_sampler</span><span class="p">)</span>

    <span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">load_study</span><span class="p">(</span><span class="n">study_name</span><span class="o">=</span><span class="n">study_name</span><span class="p">,</span> <span class="n">storage</span><span class="o">=</span><span class="n">storage</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">sampler_adapter</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">MultiObjectiveStudy</span><span class="p">(</span><span class="n">study</span><span class="p">)</span></div>


<div class="viewcode-block" id="MultiObjectiveStudy"><a class="viewcode-back" href="../../../reference/multi_objective/study.html#optuna.multi_objective.study.MultiObjectiveStudy">[文档]</a><span class="nd">@experimental</span><span class="p">(</span><span class="s2">&quot;1.4.0&quot;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MultiObjectiveStudy</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A study corresponds to a multi-objective optimization task, i.e., a set of trials.</span>

<span class="sd">    This object provides interfaces to run a new</span>
<span class="sd">    :class:`~optuna.multi_objective.trial.Trial`, access trials&#39;</span>
<span class="sd">    history, set/get user-defined attributes of the study itself.</span>

<span class="sd">    Note that the direct use of this constructor is not recommended.</span>
<span class="sd">    To create and load a study, please refer to the documentation of</span>
<span class="sd">    :func:`~optuna.multi_objective.study.create_study` and</span>
<span class="sd">    :func:`~optuna.multi_objective.study.load_study` respectively.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">:</span> <span class="n">Study</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_study</span> <span class="o">=</span> <span class="n">study</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_directions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">study</span><span class="o">.</span><span class="n">system_attrs</span><span class="p">[</span><span class="s2">&quot;multi_objective:study:directions&quot;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">d</span> <span class="o">==</span> <span class="s2">&quot;minimize&quot;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_directions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">StudyDirection</span><span class="o">.</span><span class="n">MINIMIZE</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">d</span> <span class="o">==</span> <span class="s2">&quot;maximize&quot;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_directions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">StudyDirection</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unknown direction (</span><span class="si">{}</span><span class="s2">) is specified.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>

        <span class="n">n_objectives</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_directions</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">n_objectives</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The number of objectives must be greater than 0.&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">_log_completed_trial</span> <span class="o">=</span> <span class="n">types</span><span class="o">.</span><span class="n">MethodType</span><span class="p">(</span>  <span class="c1"># type: ignore</span>
            <span class="n">_log_completed_trial</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_study</span>
        <span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">n_objectives</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Return the number of objectives.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Number of objectives.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_directions</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">directions</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">StudyDirection</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Return the optimization direction list.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A list that contains the optimization direction for each objective value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_directions</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">sampler</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;multi_objective.samplers.BaseMultiObjectiveSampler&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Return the sampler.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A :class:`~multi_objective.samplers.BaseMultiObjectiveSampler` object.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">adapter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">sampler</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">adapter</span><span class="p">,</span> <span class="n">multi_objective</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">_MultiObjectiveSamplerAdapter</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">adapter</span><span class="o">.</span><span class="n">_mo_sampler</span>

<div class="viewcode-block" id="MultiObjectiveStudy.optimize"><a class="viewcode-back" href="../../../reference/multi_objective/study.html#optuna.multi_objective.study.MultiObjectiveStudy.optimize">[文档]</a>    <span class="k">def</span> <span class="nf">optimize</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">objective</span><span class="p">:</span> <span class="n">ObjectiveFuncType</span><span class="p">,</span>
        <span class="n">timeout</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">n_trials</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">n_jobs</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
        <span class="n">catch</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[()],</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Type</span><span class="p">[</span><span class="ne">Exception</span><span class="p">]]]</span> <span class="o">=</span> <span class="p">(),</span>
        <span class="n">callbacks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">CallbackFuncType</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">gc_after_trial</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
        <span class="n">show_progress_bar</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Optimize an objective function.</span>

<span class="sd">        This method is the same as :func:`optuna.study.Study.optimize` except for</span>
<span class="sd">        taking an objective function that returns multi-objective values as the argument.</span>

<span class="sd">        Please refer to the documentation of :func:`optuna.study.Study.optimize`</span>
<span class="sd">        for further details.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="nf">mo_objective</span><span class="p">(</span><span class="n">trial</span><span class="p">:</span> <span class="n">Trial</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
            <span class="n">mo_trial</span> <span class="o">=</span> <span class="n">multi_objective</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">MultiObjectiveTrial</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span>
            <span class="n">values</span> <span class="o">=</span> <span class="n">objective</span><span class="p">(</span><span class="n">mo_trial</span><span class="p">)</span>
            <span class="n">mo_trial</span><span class="o">.</span><span class="n">_report_complete_values</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
            <span class="k">return</span> <span class="mf">0.0</span>  <span class="c1"># Dummy value.</span>

        <span class="c1"># Wraps a multi-objective callback so that we can pass it to the `Study.optimize` method.</span>
        <span class="k">def</span> <span class="nf">wrap_mo_callback</span><span class="p">(</span><span class="n">callback</span><span class="p">:</span> <span class="n">CallbackFuncType</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Callable</span><span class="p">[[</span><span class="n">Study</span><span class="p">,</span> <span class="n">FrozenTrial</span><span class="p">],</span> <span class="kc">None</span><span class="p">]:</span>
            <span class="k">return</span> <span class="k">lambda</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">:</span> <span class="n">callback</span><span class="p">(</span>
                <span class="n">MultiObjectiveStudy</span><span class="p">(</span><span class="n">study</span><span class="p">),</span>
                <span class="n">multi_objective</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">FrozenMultiObjectiveTrial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n_objectives</span><span class="p">,</span> <span class="n">trial</span><span class="p">),</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="n">callbacks</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">wrapped_callbacks</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">wrapped_callbacks</span> <span class="o">=</span> <span class="p">[</span><span class="n">wrap_mo_callback</span><span class="p">(</span><span class="n">callback</span><span class="p">)</span> <span class="k">for</span> <span class="n">callback</span> <span class="ow">in</span> <span class="n">callbacks</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span>
            <span class="n">mo_objective</span><span class="p">,</span>
            <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">,</span>
            <span class="n">n_trials</span><span class="o">=</span><span class="n">n_trials</span><span class="p">,</span>
            <span class="n">n_jobs</span><span class="o">=</span><span class="n">n_jobs</span><span class="p">,</span>
            <span class="n">catch</span><span class="o">=</span><span class="n">catch</span><span class="p">,</span>
            <span class="n">callbacks</span><span class="o">=</span><span class="n">wrapped_callbacks</span><span class="p">,</span>
            <span class="n">gc_after_trial</span><span class="o">=</span><span class="n">gc_after_trial</span><span class="p">,</span>
            <span class="n">show_progress_bar</span><span class="o">=</span><span class="n">show_progress_bar</span><span class="p">,</span>
        <span class="p">)</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">user_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Return user attributes.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dictionary containing all user attributes.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">user_attrs</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">system_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Return system attributes.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dictionary containing all system attributes.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">system_attrs</span>

<div class="viewcode-block" id="MultiObjectiveStudy.set_user_attr"><a class="viewcode-back" href="../../../reference/multi_objective/study.html#optuna.multi_objective.study.MultiObjectiveStudy.set_user_attr">[文档]</a>    <span class="k">def</span> <span class="nf">set_user_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="n">Any</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Set a user attribute to the study.</span>

<span class="sd">        Args:</span>
<span class="sd">            key: A key string of the attribute.</span>
<span class="sd">            value: A value of the attribute. The value should be JSON serializable.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">set_user_attr</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">set_system_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="n">Any</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Set a system attribute to the study.</span>

<span class="sd">        Note that Optuna internally uses this method to save system messages. Please use</span>
<span class="sd">        :func:`~optuna.multi_objective.study.MultiObjectiveStudy.set_user_attr`</span>
<span class="sd">        to set users&#39; attributes.</span>

<span class="sd">        Args:</span>
<span class="sd">            key: A key string of the attribute.</span>
<span class="sd">            value: A value of the attribute. The value should be JSON serializable.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">set_system_attr</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

<div class="viewcode-block" id="MultiObjectiveStudy.enqueue_trial"><a class="viewcode-back" href="../../../reference/multi_objective/study.html#optuna.multi_objective.study.MultiObjectiveStudy.enqueue_trial">[文档]</a>    <span class="k">def</span> <span class="nf">enqueue_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Enqueue a trial with given parameter values.</span>

<span class="sd">        You can fix the next sampling parameters which will be evaluated in your</span>
<span class="sd">        objective function.</span>

<span class="sd">        Please refer to the documentation of :func:`optuna.study.Study.enqueue_trial`</span>
<span class="sd">        for further details.</span>

<span class="sd">        Args:</span>
<span class="sd">            params:</span>
<span class="sd">                Parameter values to pass your objective function.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">enqueue_trial</span><span class="p">(</span><span class="n">params</span><span class="p">)</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">trials</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="s2">&quot;multi_objective.trial.FrozenMultiObjectiveTrial&quot;</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Return all trials in the study.</span>

<span class="sd">        The returned trials are ordered by trial number.</span>

<span class="sd">        This is a short form of ``self.get_trials(deepcopy=True)``.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A list of :class:`~optuna.multi_objective.trial.FrozenMultiObjectiveTrial` objects.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trials</span><span class="p">(</span><span class="n">deepcopy</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

<div class="viewcode-block" id="MultiObjectiveStudy.get_trials"><a class="viewcode-back" href="../../../reference/multi_objective/study.html#optuna.multi_objective.study.MultiObjectiveStudy.get_trials">[文档]</a>    <span class="k">def</span> <span class="nf">get_trials</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">deepcopy</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="s2">&quot;multi_objective.trial.FrozenMultiObjectiveTrial&quot;</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Return all trials in the study.</span>

<span class="sd">        The returned trials are ordered by trial number.</span>

<span class="sd">        For library users, it&#39;s recommended to use more handy</span>
<span class="sd">        :attr:`~optuna.multi_objective.study.MultiObjectiveStudy.trials`</span>
<span class="sd">        property to get the trials instead.</span>

<span class="sd">        Args:</span>
<span class="sd">            deepcopy:</span>
<span class="sd">                Flag to control whether to apply ``copy.deepcopy()`` to the trials.</span>
<span class="sd">                Note that if you set the flag to :obj:`False`, you shouldn&#39;t mutate</span>
<span class="sd">                any fields of the returned trial. Otherwise the internal state of</span>
<span class="sd">                the study may corrupt and unexpected behavior may happen.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A list of :class:`~optuna.multi_objective.trial.FrozenMultiObjectiveTrial` objects.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">[</span>
            <span class="n">multi_objective</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">FrozenMultiObjectiveTrial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n_objectives</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">get_trials</span><span class="p">(</span><span class="n">deepcopy</span><span class="o">=</span><span class="n">deepcopy</span><span class="p">)</span>
        <span class="p">]</span></div>

<div class="viewcode-block" id="MultiObjectiveStudy.get_pareto_front_trials"><a class="viewcode-back" href="../../../reference/multi_objective/study.html#optuna.multi_objective.study.MultiObjectiveStudy.get_pareto_front_trials">[文档]</a>    <span class="k">def</span> <span class="nf">get_pareto_front_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="s2">&quot;multi_objective.trial.FrozenMultiObjectiveTrial&quot;</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Return trials located at the pareto front in the study.</span>

<span class="sd">        A trial is located at the pareto front if there are no trials that dominate the trial.</span>
<span class="sd">        It&#39;s called that a trial ``t0`` dominates another trial ``t1`` if</span>
<span class="sd">        ``all(v0 &lt;= v1) for v0, v1 in zip(t0.values, t1.values)`` and</span>
<span class="sd">        ``any(v0 &lt; v1) for v0, v1 in zip(t0.values, t1.values)`` are held.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A list of :class:`~optuna.multi_objective.trial.FrozenMultiObjectiveTrial` objects.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">pareto_front</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">trials</span> <span class="o">=</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">trials</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">state</span> <span class="o">==</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span><span class="p">]</span>

        <span class="c1"># TODO(ohta): Optimize (use the fast non dominated sort defined in the NSGA-II paper).</span>
        <span class="k">for</span> <span class="n">trial</span> <span class="ow">in</span> <span class="n">trials</span><span class="p">:</span>
            <span class="n">dominated</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">for</span> <span class="n">other</span> <span class="ow">in</span> <span class="n">trials</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">other</span><span class="o">.</span><span class="n">_dominates</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">directions</span><span class="p">):</span>
                    <span class="n">dominated</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="k">break</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">dominated</span><span class="p">:</span>
                <span class="n">pareto_front</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">pareto_front</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">_storage</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">BaseStorage</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_study</span><span class="o">.</span><span class="n">_storage</span></div>


<span class="k">def</span> <span class="nf">_log_completed_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">:</span> <span class="n">Study</span><span class="p">,</span> <span class="n">trial</span><span class="p">:</span> <span class="n">Trial</span><span class="p">,</span> <span class="n">result</span><span class="p">:</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="n">values</span> <span class="o">=</span> <span class="n">multi_objective</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">MultiObjectiveTrial</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span><span class="o">.</span><span class="n">_get_values</span><span class="p">()</span>
    <span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
        <span class="s2">&quot;Trial </span><span class="si">{}</span><span class="s2"> finished with values: </span><span class="si">{}</span><span class="s2"> with parameters: </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="n">trial</span><span class="o">.</span><span class="n">number</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">params</span><span class="p">,</span>
        <span class="p">)</span>
    <span class="p">)</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>